package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.ApprovalStepsMapper;
import com.zygh.policeasset.mapper.AssetsUpdateMapper;
import com.zygh.policeasset.mapper.AuditFlowDetailMapper;
import com.zygh.policeasset.pojo.*;
import com.zygh.policeasset.pojo.vo.AssetsUpdateVO;
import com.zygh.policeasset.utils.AuditUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


@Service
public class AssetsUpdateService {
    @Autowired
    AssetsUpdateMapper assetsUpdateMapper;

    @Autowired
    AuditFlowService auditFlowService;

    @Autowired
    AuditFlowDetailMapper auditFlowDetailMapper;

    @Autowired
    ApprovalStepsMapper approvalStepsMapper;

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult add(AssetsUpdate assetsUpdate) {
        try {
            Integer count = assetsUpdateMapper.findCountById(assetsUpdate.getAssetId());

            if (count > 0) {
                return new CommonResult().validateFailed("此资产已提交申请");
            }

            String now = DateUtil.now();
            String flowNo = AuditUtil.getFlowNo(now);
            assetsUpdate.setFlowNo(flowNo);

            //线上环境使用,不要随意改动
            auditFlowService.addYouKe(169, flowNo, now, TableType.ASSETSUPDATE.getName(),
                    TableType.ASSETSUPDATE.getType(), null, null, 28);

            this.auditFlowDetailMapper.insert(new AuditFlowDetail()
                    .setFlowNo(flowNo)
                    .setReviewer(106)
                    .setAuditStatus(Integer.valueOf(AuditStatusEnum.SUBMIT.getIndex()))
                    .setAuditTime(DateUtil.now()));

            String[] users = approvalStepsMapper.approvedById(20).split(",");
            Integer auditStatus;
            for (int i = 0; i < users.length; i++) {
                if (i == 0) {
                    auditStatus = Integer.valueOf(AuditStatusEnum.FORMYAPPROVAL.getIndex());
                } else {
                    auditStatus = Integer.valueOf(AuditStatusEnum.WAITAPPROVAL.getIndex());
                }
                this.auditFlowDetailMapper.insert(new AuditFlowDetail()
                        .setFlowNo(flowNo)
                        .setReviewer(Integer.valueOf(Integer.parseInt(users[i])))
                        .setAuditStatus(auditStatus));
            }
            Integer res = assetsUpdateMapper.add(assetsUpdate);
            if (res > 0) {
                return new CommonResult().success();
            }
            return new CommonResult().validateFailed("添加错误");

        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public CommonResult findById(Integer id) {
        return new CommonResult().success(assetsUpdateMapper.findById(id));
    }
}